package cn.dy.sbo.web.cus;

import cn.common.lib.springside.orm.ExtPropertyFilter;
import cn.dy.sbo.contants.Constants;
import cn.dy.sbo.core.DraftOrdersManager;
import cn.dy.sbo.core.FlowTypeManager;
import cn.dy.sbo.core.PartnerManager;
import cn.dy.sbo.core.UserManager;
import cn.dy.sbo.model.DraftOrders;
import cn.dy.sbo.model.FlowType;
import cn.dy.sbo.model.Partner;
import cn.dy.sbo.model.User;
import cn.dy.sbo.util.CommonUtils;
import cn.dy.sbo.vo.DropListVO;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springside.modules.orm.Page;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import java.util.Date;
import java.util.List;

@Namespace("/cus")
public class DraftAction extends ActionSupport  implements Preparable{

    private static final long serialVersionUID = 1L;

    private Long id;
    private Long flowTypeId;

    protected Page<DraftOrders>         page             = new Page<DraftOrders>(10);

    private DraftOrders draft;

    @Autowired
    private DraftOrdersManager ordersManager;

    @Autowired
	private FlowTypeManager flowTypeManager;

    @Autowired
    private PartnerManager partnerManager;
    
    @Autowired
    private UserManager userManager;

    @Autowired
    private MessageSource messages;

    @Override
    public String execute() throws Exception
    {
    	List<ExtPropertyFilter> filters = ExtPropertyFilter
        .extBuildFromHttpRequest(Struts2Utils.getRequest());
    	UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User u = userManager.getByUserName(userDetails.getUsername());
    	filters.add(new ExtPropertyFilter("EQL_id",String.valueOf(u.getPartner().getId()),"partner"));
    	page = ordersManager.extSearch(page, filters);
        return SUCCESS;
    }

    public String newdraft(){
    	draft = new DraftOrders();
    	draft.setBiztype(1L);
    	return "new";
    }

    public String save(){
    	UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User u = userManager.getByUserName(userDetails.getUsername());
        Partner p =  u.getPartner() ;
        draft.setCusName(p.getCardName());
        draft.setCusCode(p.getCardCode());
        draft.setPartner(p);
        if(flowTypeId != null)   {
            FlowType flowType = flowTypeManager.get(flowTypeId);
            draft.setFlowType(flowType);
        }
        draft.setOrderDate(new Date());
        draft.setCreateDate(new Date());
        draft.setStatus(Constants.DRAFT_STATUS_NEW);
        
        draft.setCreater(u);
        ordersManager.save(draft);
        addActionMessage(messages.getMessage("msg.addsuceess",null, CommonUtils.getLocal()));
        return "new";
    }

    public String detail(){
        draft = ordersManager.get(id);
        return "detail";
    }

    public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Page<DraftOrders> getPage() {
		return page;
	}

	public void setPage(Page<DraftOrders> page) {
		this.page = page;
	}

	public DraftOrders getDraft() {
		return draft;
	}

	public void setDraft(DraftOrders draft) {
		this.draft = draft;
	}

	public void prepareSave() throws Exception
    {
        prepare();
    }

    public void prepareInput() throws Exception
    {
        prepare();
    }

    @Override
    public void prepare() throws Exception
    {
        if (null != id)
        {
        	draft = ordersManager.get(id);
        }
        else
        {
            draft = new DraftOrders();
        }
    }

    public Long getFlowTypeId() {
        return flowTypeId;
    }

    public void setFlowTypeId(Long flowTypeId) {
        this.flowTypeId = flowTypeId;
    }
}